package com.hjw.hjwmall.product.controller;

import java.util.List;

import org.springframework.web.bind.annotation.*;

import com.hjw.hjwmall.product.entity.CategoryEntity;
import com.hjw.hjwmall.product.service.CategoryService;
import com.hjw.common.utils.R;

import javax.annotation.Resource;

/**
 * 商品三级分类
 *
 * @author hjw
 * @email hjw@gmail.com
 * @date 2021-08-02 20:55:58
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 查找所有分类及其子类列表，以树形结构组装起来
     */
    @GetMapping("/list/tree")
    public R list() {
        List<CategoryEntity> categoryList = categoryService.listWithTree();
        return R.ok().put("categoryList", categoryList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        if (categoryService.saveCategory(category)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody List<Long> catIds) {
        if (categoryService.removeCategoryByIds(catIds)) {
            return R.ok();
        }
        return R.error();
    }

}
